﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using QuanLiThuVien.Source;
using QuanLiThuVien.LinkModel;

namespace QuanLiThuVien
{
    /// <summary>
    /// Interaction logic for DanhSachNhanVien.xaml
    /// </summary>
    public partial class DanhSachNhanVien : Page
    {
        private InsertInfo m_insertInfo = new InsertInfo();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private DeleteInfo m_deleteInfo = new DeleteInfo();
        private ShowInfo m_showInfo = new ShowInfo();

        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        static bool m_bInsertData = false;

        public DanhSachNhanVien()
        {
            InitializeComponent();
            addCmbLoaiNV(cmbLoaiNV);
            m_showInfo.ShowListNhanVien(dataNhanVien);
        }

        private void addCmbLoaiNV(ComboBox m_cmbTheDocGia)
        {
            m_cmbTheDocGia.Items.Add("Thủ Thư");
            m_cmbTheDocGia.Items.Add("Thủ Kho");
        }

        private void ClearIndex()
        {
            txtHoTenNV.Text = "";
            cmbLoaiNV.SelectedItem = null;
        }

        private bool KiemTraNhap()
        {
            if (txtHoTenNV.Text == ""
                || cmbLoaiNV.SelectedItem == null)
                return false;
            else return true;
        }

        private void bntThemNV_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap() == true)
            {
                string m_sMaLoaiNV = serv.SelectMaLoaiNhanVien(cmbLoaiNV.SelectedItem.ToString()).ToString();
                m_insertInfo.InsertNhanVien(m_sMaLoaiNV, txtHoTenNV.Text);
                m_showInfo.ShowListNhanVien(dataNhanVien);
                ClearIndex();
            }
            else
            {
                MessageBox.Show("Phải nhập thông tin đầy đủ trước khi lập thẻ", "Lỗi");
            }
        }

        private void bntXoaNV_Click(object sender, RoutedEventArgs e)
        {
            var item = dataNhanVien.SelectedItem as DSNhanVien;
            m_deleteInfo.DeleteNhanVien(item.MaNhanVien);
            if (!m_bInsertData)
                m_bInsertData = true;
            m_showInfo.ShowListNhanVien(dataNhanVien);
        }

        private void bntSuaNV_Click(object sender, RoutedEventArgs e)
        {
            m_bInsertData = true;
            var item = dataNhanVien.SelectedItem as DSNhanVien;
            string m_sMaLoaiNV = serv.SelectMaLoaiNhanVien(cmbLoaiNV.SelectedItem.ToString()).ToString();
            string m_sMaNhanVien = item.MaNhanVien.ToString();
            m_updateInfo.UpdateNhanVien(m_sMaNhanVien, m_sMaLoaiNV, txtHoTenNV.Text);
            if (!m_bInsertData)
                m_bInsertData = true;
            ClearIndex();
            dataNhanVien.ItemsSource = null;
            m_showInfo.ShowListNhanVien(dataNhanVien);
        }

        private void dataNhanVien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ClearIndex();
            bntThemNV.IsEnabled = false;
            if (!m_bInsertData)
            {
                var item = dataNhanVien.SelectedItem as DSNhanVien;
                txtHoTenNV.Text = item.HoTenNhanVien.ToString();
                cmbLoaiNV.SelectedItem = item.TenLoaiNhanVien.ToString();
            }
            else
            {
                m_bInsertData = false;
                m_showInfo.ShowListDocGia(dataNhanVien);
            }
        }

        private void bntRefresh_Click(object sender, RoutedEventArgs e)
        {
            if (!m_bInsertData)
                m_bInsertData = true;
            ClearIndex();
            dataNhanVien.ItemsSource = null;
            bntThemNV.IsEnabled = true;
            m_bInsertData = false;
            m_showInfo.ShowListNhanVien(dataNhanVien);
        }
    }
}
